package tech.abing.spider.task.crawler.amac;

import java.util.Arrays;
import java.util.List;

import javax.management.JMException;

import org.apache.log4j.Logger;

import tech.abing.spider.CrawTaskBuilder;
import tech.abing.spider.CrawlManager;
import tech.abing.spider.CrawlTask;
import tech.abing.spider.component.DynamicEntrance;
import tech.abing.spider.component.listener.TaskLifeListener;
import tech.abing.spider.downloader.impl.DefaultPageDownloader;
import tech.abing.spider.monitor.SpiderMonitor;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.PageRequest.Method;
import tech.abing.spider.request.PageRequest.PageEncoding;
import tech.abing.spider.request.PageRequest.PostType;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.DictEnum;
import tech.abing.spider.task.pageprocessor.amac.ManagerProcessor;
import tech.abing.spider.task.pipeline.amac.ManagerPipeline;
import tech.abing.spider.util.SqlUtil;

/**
 * 抓取私募基金管理人
 * @author Ivan qb.huang@wescxx.com   
 * @date 2016-10-31 下午02:53:59 
 * @version V2.0
 */
public class ManagerSpider {
	
	private static final Logger log = Logger.getLogger(ManagerSpider.class);

	/**
	 * @param args
	 */
	private static SqlUtil sqlUtil = null;
	
	public static void start(int thread,int politenessDelay){
		
		CrawlManager manager = CrawlManager.getInstance();
		
		CrawTaskBuilder builder = manager
		.prepareCrawlTask(DictEnum.TASK_TYPE_FUND_MANAGER.getName(), DefaultPageDownloader.class)
		.useThread(thread)
		.useDynamicEntrance(DynamicEntranceImpl.class)
		.usePageEncoding(PageEncoding.UTF8)
		.usePageRetryCount(3)
		.usePipeline(ManagerPipeline.class)
		.setPolitenessDelay(politenessDelay)
//		.useProxyIpPool(MyProxyIpPool.class, 100, 1000 * 60 * 30, -1)//每次网络请求10个代理ip,ip30分钟后过期,有效的ip不限制使用次数
		.useTaskLifeListener(new TaskLifeListener(){

			@Override
			public void onStart(CrawlTask task) {
				log.info("================开始================");
				if(sqlUtil==null){
					sqlUtil = SqlUtil.getInstance();
				}
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_EXECUTIVES.getCode());
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_CURRICULUM_VITAE.getCode());
				sqlUtil.deleteForConn("delete from "+DictEnum.TASK_TYPE_FUND_MANAGER.getCode());
				sqlUtil.deleteForConn("delete from manager_stat");
			}

			@Override
			public void onFinished(CrawlTask task) {
				task.getPipeline().destory();
				sqlUtil.closeConn();
				sqlUtil = null;
				log.info("================完成================");
			}
			
		});
		
		CrawlTask task = builder.build();
		
		try {
			SpiderMonitor.instance().register(task);
		} catch (JMException e) {
			e.printStackTrace();
		}
		
		manager.start(task);
	}
	
	public static void start() {
		start(20,60);
	}
	
	public static final class DynamicEntranceImpl extends DynamicEntrance{

		@Override
		public List<StartContext> loadStartContext() {
			
			StartContext context = new StartContext();
			
			String rand = String.valueOf(Math.random());
			String url = "http://gs.amac.org.cn/amac-infodisc/api/pof/manager?rand="+rand+"&page=0&size=20";
			
			PageRequest req = context.createPageRequest(url, ManagerProcessor.class);
			req.setMethod(Method.POST);
			req.setPostType(PostType.REQUEST_PAYLOAD);
			req.setReqPayloadParams("{}");
			
			req.putHeader("Content-Type", "application/json; charset=UTF-8");
			req.putHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko");
			req.putHeader("Referer", "http://gs.amac.org.cn/amac-infodisc/res/pof/manager/index.html");
			req.putHeader("X-Requested-With", "XMLHttpRequest");
			req.putHeader("Host", "gs.amac.org.cn");
			req.putHeader("Accept", "application/json, text/javascript, */*; q=0.01");
			req.putHeader("Accept-Language", "zh-CN,zh;q=0.8");
			req.putHeader("Cache-Control", "no-cache");
			
			
			context.injectSeed(req);
			
			return Arrays.asList(context);
		}
		
	}

}
